package com.zhou.base.components.mp.ds.template.config;

import cn.dev33.satoken.context.SaHolder;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zhou.base.components.mp.ds.login.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import static com.zhou.base.components.mp.ds.helper.LoginHelper.LOGIN_USER_KEY;

/**
 * @author fenghua
 * @version 1.0
 * @date 2023/2/16 5:35 下午
 */
@Slf4j
@Component
public class MybatisDataFillHandler implements MetaObjectHandler {


    //插入时的填充策略
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "creatorId", String.class, "creatorId");
        this.strictInsertFill(metaObject, "creatorName", String.class, "creatorName");
        this.strictInsertFill(metaObject, "modifierId", String.class, "modifierId");
        this.strictInsertFill(metaObject, "modifierName", String.class, "modifierName");

        LoginUser loginUser = (LoginUser) SaHolder.getStorage().get(LOGIN_USER_KEY);
        if (loginUser != null) {
            this.strictInsertFill(metaObject, "userId", Long.class, loginUser.getUserId());
            this.strictInsertFill(metaObject, "deptId", Long.class, loginUser.getDeptId());
        }

    }

    //更新时的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "modifierId", String.class, "modifierId");
        this.strictUpdateFill(metaObject, "modifierName", String.class, "modifierName");
    }

}
